#!/bin/bash
DEBIANVERSION=$(cat /etc/debian_version | cut -d. -f1)


function showicon_running {  
  RUNNING=1
  IMGNUM=10
  test $SHOWICON -lt 2 && echo "visible:true"
  echo "tooltip:Update-Suche / Software-Installation läuft!"
  while [ $RUNNING -gt 0 ]; do
    IMGNUM=$(($IMGNUM+1));
    if [ $IMGNUM -gt 22 ]; then
      IMGNUM=11
      RUNNING=$(pgrep -c -x "apt|apt-get|dpkg|xlinux-update|xlinux-update-check" 2>/dev/null)
    fi
    echo "icon:xlinux-spinner-$IMGNUM"    
    sleep 0.1    
  done  
}


function showicon {  
  while true; do
    MENU=""    
    RUNNING=$(pgrep -c -x "apt|apt-get|dpkg|xlinux-update|xlinux-update-check" 2>/dev/null)
    if [ $RUNNING -gt 0 ]; then
      showicon_running
    fi
    ICON="xlinux-ok"
    UPDATE_FAILED_SOURCES=$(cat /var/cache/xlinux/last_update_check_failed_sources || echo 0) 
    UPDATE_FAILED_SOURCES=0
    UPDATE_LAST_CHECK=$(cat /var/cache/xlinux/last_update_check || echo "unbekant") 
    UPDATE_LAST_SUCCESS=$(cat /var/cache/xlinux/last_update_success || echo "unbekannt") 
    UPDATE_AVAILABLE=$(/usr/bin/apt-show-versions -u | wc -l)
    UPDATE_TEXT="Das Linux-System ist aktuell."
    if [ $UPDATE_FAILED_SOURCES -gt 0 ]; then
      UPDATE_TEXT="Problem mit Paketquellen."
      ICON="xlinux-warn"
    fi
    if [ $UPDATE_AVAILABLE -gt 0 ]; then
      UPDATE_TEXT="$UPDATE_AVAILABLE Update(s) zur Installation bereit!"
      MENU="${MENU}|$UPDATE_AVAILABLE Update(s) jetzt installieren!xlinux-update --force --install --allyes --showdesktopmessages"
      ICON="xlinux-updates-available"
      #if [ $DEBIANVERSION -ge 13 ]; then
      #  update-meldung.py &
      #fi
    fi
    echo "tooltip:$UPDATE_TEXT"
    echo "icon:$ICON"
    #Update starten!/opt/XLinux/softwarecenter/updates.sh
    SHOWICON=$(xlinux-settings get XLINUX_STATUSICON_SHOW || echo 0)
    visible=true
    test $SHOWICON -gt 0 -a "x$ICON" == "xxlinux-ok" && visible=false
    test $SHOWICON -eq 2 -a "x$ICON" != "xxlinux-warn" && visible=false
    echo "visible:$visible"
    #echo "menu:Software-Center starten!/opt/XLinux/softwarecenter/softwarecenter.sh|Updates jetzt suchen!xlinux-update-check --force$MENU|Fernwartung starten!/opt/XLinux-Fernwartung/2022_fernwartung.sh|Beenden!quit"
    #echo "menu:Software-Center starten!/opt/XLinux/softwarecenter/Softwarecenter.py|Updates jetzt suchen!xlinux-update-check --force$MENU|Fernwartung starten!/opt/XLinux-Fernwartung/2022_fernwartung.sh|Beenden!quit"
    echo "menu:Software-Center starten!/opt/XLinux/softwarecenter/Softwarecenter.py|Updates jetzt suchen!xlinux-update --search --force --showdesktopmessages$MENU|Fernwartung starten!/opt/XLinux-Fernwartung/2022_fernwartung.sh|Beenden!quit"
    if [ $RUNNING -eq 0 ]; then
      inotifywait -t 300 -e modify /var/cache/xlinux /var/lib/apt/ /var/lib/dpkg/ >/dev/null 2>&1
    else
      sleep 2
    fi
    #while [ 1 ]; do clear; pgrep -x "apt|apt-get|dpkg" ; echo $? ; sleep 1 ; done
  done | yad --notification --listen --command="python3 /usr/bin/xlinux-admintool.py" --image="xlinux-ok" --no-middle
}


showicon
